
#include "DynamicSoundEffectInstance.h"

#include <System/EventHandler.h>
#include <System/Threading/Thread.h>
#include <Riccsson/Xna/Framework/Audio/_SoundEngineWrapper.h>
#include <Riccsson/Xna/Framework/RGame.h>

using namespace Riccsson::System;
using namespace Riccsson::Xna::Framework;
using namespace Riccsson::Xna::Framework::Audio;

DynamicSoundEffectInstance::DynamicSoundEffectInstance(int sampleRate, AudioChannels channels)
	: PROP3GET_INIT(DynamicSoundEffectInstance, PendingBufferCount)
{
	init(sampleRate, channels);

	this->m_soundEngine = _SoundEngineWrapper::getInstance();

	this->m_soundEngine->OnBuffer += new System::EventHandler(this, &DynamicSoundEffectInstance::_OnBufferNeeded);

	this->m_soundEngine->play();
}

Void DynamicSoundEffectInstance::_OnBufferNeeded(GC_PTR<Object> sender, GC_PTR<EventArgs> args)
{
	// If the game has been fully initialized.
	// Otherwise the sound buffer event will run before the
	// initializing has been finished. Thats not good.
	if (Game::initializedComplete)
	{
		if (BufferNeeded != null)
		{
			BufferNeeded(this, EventArgs::Empty);
		}
	}
	else
	{
		// Don't overload the CPU.
		System::Threading::Thread::Sleep(50);
	}

	return Void();
}

void DynamicSoundEffectInstance::Dispose(bool disposing)
{
	throw;
}
Riccsson::System::TimeSpan DynamicSoundEffectInstance::GetSampleDuration(int sizeInBytes)
{
	throw;
}
int DynamicSoundEffectInstance::GetSampleSizeInBytes(Riccsson::System::TimeSpan duration)
{
	throw;
}
void DynamicSoundEffectInstance::Play()
{

}
void DynamicSoundEffectInstance::SubmitBuffer(Riccsson::System::Array<byte>* buffer)
{
	this->m_soundEngine->submitBuffer(buffer->get(), buffer->Length);
}
void DynamicSoundEffectInstance::SubmitBuffer(byte* buffer)
{

}
void DynamicSoundEffectInstance::SubmitBuffer(Riccsson::System::Array<byte>* buffer, int offset, int count)
{
	throw;
}